﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Sabnzbd
{
    public class MovieFileNameManipulation
    {
        public List<MovieFile> GetMoviesForDirectory(string path)
        {
            return GetMoviesForDirectory(path, "*");
        }

        public List<MovieFile> GetMoviesForDirectory(string path, string matchingFilename)
        {
            List<MovieFile> movies = new List<MovieFile>();
            string[] avis = Directory.GetFiles(path, matchingFilename + ".avi");
            string[] mkvs = Directory.GetFiles(path, matchingFilename + ".mkv");
            foreach (string movie in avis)
            {
                MovieFile _movie = new MovieFile();
                _movie.MovieType = MovieType.Xvid;
                _movie.FileName = movie;
                movies.Add(_movie);
            }
            foreach (string movie in mkvs)
            {
                MovieFile _movie = new MovieFile();
                _movie.MovieType = MovieType.MKV;
                _movie.FileName = movie;
                movies.Add(_movie);
            }
            return movies;
        }

        public bool CheckForDoubleMovies(List<MovieFile> movies)
        {
            if (movies.Count != 2)
                return false;

            string file1 = Path.GetFileNameWithoutExtension(movies[0].FileName);
            string file2 = Path.GetFileNameWithoutExtension(movies[1].FileName);

            return file1.Equals(file2);
        }

        public List<MovieFile> RenameDoubleMovies(List<MovieFile> movies, string aviSuffix, string mkvSuffix)
        {
            if (!CheckForDoubleMovies(movies))
                throw new Exception("No double movies found");

            string file1 = Path.GetFileNameWithoutExtension(movies[0].FileName);
            string file2 = Path.GetFileNameWithoutExtension(movies[1].FileName);

            string file1ext = Path.GetExtension(movies[0].FileName);
            string file2ext = Path.GetExtension(movies[1].FileName);

            string file1path = Path.GetDirectoryName(movies[0].FileName);
            string file2path = Path.GetDirectoryName(movies[1].FileName);

            List<MovieFile> _movies = new List<MovieFile>();
            if (file1ext.Equals(".mkv"))
            {
                MovieFile _movie1 = new MovieFile();
                _movie1.FileName = Path.Combine(file1path, file1) + mkvSuffix + file1ext;
                _movie1.MovieType = MovieType.MKV;
                MovieFile _movie2 = new MovieFile();
                _movie2.FileName = Path.Combine(file2path, file2) + aviSuffix + file2ext;
                _movie2.MovieType = MovieType.MKV;
                File.Move(movies[0].FileName, _movie1.FileName);
                File.Move(movies[1].FileName, _movie2.FileName);
                _movies.Add(_movie1);
                _movies.Add(_movie2);
            }
            else
            {
                MovieFile _movie1 = new MovieFile();
                _movie1.FileName = Path.Combine(file1path, file1) + aviSuffix + file1ext;
                _movie1.MovieType = MovieType.MKV;
                MovieFile _movie2 = new MovieFile();
                _movie2.FileName = Path.Combine(file2path, file2) + mkvSuffix + file2ext;
                _movie2.MovieType = MovieType.MKV;
                File.Move(movies[0].FileName, _movie1.FileName);
                File.Move(movies[1].FileName, _movie2.FileName);
                _movies.Add(_movie1);
                _movies.Add(_movie2);
            }

            return _movies;
        }
    }
}
